package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.model.entity.RefundRecord;
import com.qf.model.entity.RefundRecord;
import com.qf.service.RefundRecordService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (RefundRecord)表控制层
 *
 * @author makejava
 * @since 2024-05-06 11:30:00
 */
@RestController
@RequestMapping("/api/refundRecord")
public class RefundRecordController {
    /**
     * 服务对象
     */
    @Autowired
    private RefundRecordService refundRecordService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @RequestMapping("/list")
    public R queryByPage(Integer pageNum) {
        return R.ok(this.refundRecordService.queryByPage(pageNum));
    }

    @RequestMapping("/list/page")
    public R selectOutpatientRechargeByCondition(@RequestParam Map<String,Object> map) {

        System.out.println("map = " + map);

        int pageNum = Integer.parseInt((String) map.get("pageNum"));
        int pageSize = Integer.parseInt((String) map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);

        List<RefundRecord> list = refundRecordService.selectOutpatientRechargeByCondition(map);
        if (list != null && !list.isEmpty()) {
            PageInfo<RefundRecord> pageInfo = new PageInfo<>(list);
            return R.ok(pageInfo);
        }

        return R.fail();
    }



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/{id}")
    public R queryById(@PathVariable("id") Integer id) {
        return R.ok(this.refundRecordService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param refundRecord 实体
     * @return 新增结果
     */
    @RequestMapping("/save")
    public R add(RefundRecord refundRecord, HttpSession session) {

        session.setAttribute("refundRecord",refundRecord);

        return R.ok(this.refundRecordService.insert(refundRecord,session));
    }

    /**
     * 编辑数据
     *
     * @param refundRecord 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit(RefundRecord refundRecord) {
        return R.ok(this.refundRecordService.update(refundRecord));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/del/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        return R.ok(this.refundRecordService.deleteById(id));
    }

}

